package com.acmen.easyapi.web;


import com.acmen.easyapi.model.CallResult;
import com.acmen.easyapi.model.PressureTest;
import com.acmen.easyapi.model.TestReport;
import com.acmen.easyapi.model.CustomizePage;
import com.acmen.easyapi.service.CallResultService;
import com.acmen.easyapi.service.PressureTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author acmen
 * 组合测试服务
 * @date 2022-03-16
 */
@Controller
public class TestReportController {
    @Autowired
    private PressureTestService pressureTestService;
    @Autowired
    private CallResultService callResultService;


    @GetMapping(value = {"/test/report"})
    public String testReport(HttpServletRequest request, ModelMap model) {
        model.put("contextPath", request.getContextPath());
        String testId = request.getParameter("testId");
        PressureTest pressureTest = pressureTestService.getById(testId);
        List<TestReport> aggregateReports = pressureTest.getAggregateReports();
        model.put("pressureTest", pressureTest);
        aggregateReports = aggregateReports.stream().sorted(Comparator.comparing(TestReport::getPath)).collect(Collectors.toList());
        pressureTest.setAggregateReports(aggregateReports);
        return "/test/report";
    }

    @GetMapping(value = {"/test/call-result"})
    public String callResult(HttpServletRequest request, ModelMap model) {
        model.put("contextPath", request.getContextPath());
        String reportId = request.getParameter("reportId");
        Integer pageNo =  request.getParameter("pageNo")==null?1:Integer.parseInt(request.getParameter("pageNo"));
        Integer pageSize = request.getParameter("pageSize")==null?100:Integer.parseInt(request.getParameter("pageSize"));;
        CustomizePage<CallResult> callResults = callResultService.queryPageList(reportId, pageSize, pageNo);
        model.put("reportId", reportId);
        model.put("callResults", callResults);
        model.put("pageSize", pageSize);
        model.put("pageNo", pageNo);
        return "/test/call-result";
    }
}
